package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.domain.ZhDeptInfo;
import com.ruoyi.system.domain.ZhGuide;
import com.ruoyi.common.service.IZhDeptInfoService;
import com.ruoyi.system.service.IZhGuideService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 新手指南 信息操作处理
 *
 * @author ruoyi
 * @date 2019-04-21
 */
@Controller
@RequestMapping("/system/zhGuide")
public class ZhGuideController extends BaseController {
    private String prefix = "system/zhGuide";

    @Autowired
    private IZhGuideService zhGuideService;
    @Autowired
    private IZhDeptInfoService zhDeptInfoService;

    @DataScope(tableAlias = "l")
    @RequiresPermissions("system:zhGuide:edit")
    @GetMapping("wangrditor")
    public String wangrditor(
                            ZhGuide zhGuide,
                             Integer id,
                             Model model) {
        //todo 随便修改id都能进来

        ZhGuide guide = zhGuideService.selectZhGuideById(id);
        model.addAttribute("item" , guide != null ? guide : new ZhGuide());
        return prefix + "/wangrditor";
    }

    @DataScope(tableAlias = "l")
    @RequiresPermissions("system:zhGuide:view")
    @GetMapping()
    public String zhGuide(ZhGuide zhGuide,ModelMap mmap) {
        mmap.put("zhDepts" , zhDeptInfoService.selectZhDeptInfoList(buildParamsContainer(ZhDeptInfo.class,zhGuide.getParams())));
        return prefix + "/zhGuide";
    }

    /**
     * 查询新手指南列表
     */
    @DataScope(tableAlias = "l")
    @RequiresPermissions("system:zhGuide:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZhGuide zhGuide,ModelMap mmap) {
        startPage();
        List<ZhGuide> list = zhGuideService.selectZhGuideList(zhGuide);
        return getDataTable(list);
    }


    /**
     * 导出新手指南列表
     */
    @DataScope(tableAlias = "l")
    @RequiresPermissions("system:zhGuide:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZhGuide zhGuide) {
        List<ZhGuide> list = zhGuideService.selectZhGuideList(zhGuide);
        ExcelUtil<ZhGuide> util = new ExcelUtil<ZhGuide>(ZhGuide.class);
        return util.exportExcel(list, "zhGuide");
    }

    /**
     * 新增新手指南
     */
    @DataScope(tableAlias = "l")
    @GetMapping("/add")
    public String add(ZhDeptInfo zhDeptInfo,ModelMap mmap) {
        mmap.put("zhDepts" , zhDeptInfoService.selectZhDeptInfoList(buildParamsContainer(ZhDeptInfo.class,zhDeptInfo.getParams())));
        return prefix + "/add";
    }

    /**
     * 新增保存新手指南
     */
    @RequiresPermissions("system:zhGuide:add")
    @Log(title = "新手指南" , businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZhGuide zhGuide) {
        return toAjax(zhGuideService.insertZhGuide(zhGuide));
    }

    /**
     * 修改新手指南
     */
    
    @GetMapping("/edit/{id}")
    public String edit(ZhDeptInfo zhDeptInfo,
                       @PathVariable("id") Integer id,
                       ModelMap mmap) {
        ZhGuide zhGuide = zhGuideService.selectZhGuideById(id);


        mmap.put("zhGuide" , zhGuide);
        mmap.put("zhDepts" , zhDeptInfoService.selectZhDeptInfoList(buildParamsContainer(ZhDeptInfo.class,zhDeptInfo.getParams())));

        return prefix + "/edit";
    }

    /**
     * 修改保存新手指南
     */
    @RequiresPermissions("system:zhGuide:edit")
    @Log(title = "新手指南" , businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZhGuide zhGuide) {
        return toAjax(zhGuideService.updateZhGuide(zhGuide));
    }

    /**
     * 删除新手指南
     */
    @DataScope(tableAlias = "l")
    @RequiresPermissions("system:zhGuide:remove")
    @Log(title = "新手指南" , businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(ZhGuide zhGuide,String ids) {
        return toAjax(zhGuideService.deleteZhGuideByIds(ids));
    }

}
